#include "syscall.h"
#include "task.h"
#include "log.h"
#include "irq.h"
#include "os_cfg.h"
#include "fs.h"
#include "mem.h"

typedef int (*syscall_handler_t)(int arg0, int arg1, int arg2, int arg3);

int syscall_printmsg(char *fmt, int arg1)
{
    log_printf(fmt, arg1);
    return 0;
}
int syscall_printmsg1(char *fmt, ...)
{
    log_printf(fmt);
    return 0;
}
static const syscall_handler_t syscall_table[256] = {[SYS_sleep]     = (syscall_handler_t)sys_sleep,
                                                     [SYS_printmsg]  = (syscall_handler_t)syscall_printmsg,
                                                     [SYS_printmsg1] = (syscall_handler_t)syscall_printmsg1,

                                                     [SYS_getpid] = (syscall_handler_t)sys_getpid,
                                                     [SYS_fork]   = (syscall_handler_t)sys_fork,
                                                     [SYS_execve] = (syscall_handler_t)sys_execve,
                                                     [SYS_yield]  = (syscall_handler_t)sys_yield,

                                                     [SYS_open]  = (syscall_handler_t)sys_open,
                                                     [SYS_read]  = (syscall_handler_t)sys_read,
                                                     [SYS_write] = (syscall_handler_t)sys_write,
                                                     [SYS_close] = (syscall_handler_t)sys_close,
                                                     [SYS_lseek] = (syscall_handler_t)sys_lseek,

                                                     [SYS_isatty] = (syscall_handler_t)sys_isatty,
                                                     [SYS_fstat]  = (syscall_handler_t)sys_fstat,
                                                     [SYS_sbrk]   = (syscall_handler_t)sys_sbrk,
                                                     [SYS_dup]    = (syscall_handler_t)sys_dup,
                                                     [SYS_exit]   = (syscall_handler_t)sys_exit,
                                                     [SYS_wait]   = (syscall_handler_t)sys_wait,

                                                     [SYS_opendir]  = (syscall_handler_t)sys_opendir,
                                                     [SYS_readdir]  = (syscall_handler_t)sys_readdir,
                                                     [SYS_closedir] = (syscall_handler_t)sys_closedir,
                                                     [SYS_ioctl]    = (syscall_handler_t)sys_ioctl,
                                                     [SYS_unlink]   = (syscall_handler_t)sys_unlink,
                                                     0};

void do_handler_syscall(syscall_frame_t *frame)
{
    if (frame->func_id < 0 || frame->func_id > sizeof(syscall_table) / sizeof(syscall_handler_t))
    {
        task_t *task = task_current_task();
        log_printf("task:%s,Unknown syscall:func_id:%d", task->name, frame->func_id);
        frame->eax = -1;
        return;
    }
    syscall_handler_t func = syscall_table[frame->func_id];
    int               ret  = func(frame->arg0, frame->arg1, frame->arg2, frame->arg3);
    frame->eax             = ret;
}

/*void do_handler_syscall(exception_frame_t * frame){
    int func_id=frame->eax;
    int arg0=frame->ebx;
    int arg1=frame->ecx;
    int arg2=frame->edx;
    int arg3=frame->esi;
    if(func_id <0 || func_id >= IDT_TABLE_NR){
        task_t *task=task_current_task();
        log_printf("task:%s,Unknown syscall:func_id:%d",task->name,func_id);
        frame->eax=-1;
        return;
    }
    syscall_handler_t func=syscall_table[func_id];
    int ret=func(arg0,arg1,arg2,arg3);
    frame->eax=ret;
}*/